package com.tao.design.iterator

import java.util.*
import kotlin.collections.ArrayList

/**
 *
 * @ProjectName:    lab
 * @Package:        com.tao.design.iterator
 * @ClassName:      Iterator
 * @Description:     java类作用描述
 * @Author:         wangxf
 * @CreateDate:     2022/4/27 9:18
 * @UpdateUser:     更新者
 * @UpdateDate:     2022/4/27 9:18
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
/**
 *
 *迭代器模式
 *
 */
interface Iterator<T> {
    fun hasMore(): Boolean;
    fun next(): T?;
    fun remove();
}

class DinerMenuIterator(val dinerMenu: DinerMenu) : Iterator<DinerMenu> {
    override fun hasMore(): Boolean {
        return true;
    }

    override fun next(): DinerMenu? {
        return dinerMenu.dinerMenu[0]
    }

    override fun remove() {
    }
}

class LunchMenuIterator(val lunchmenu: Lunchmenu) : Iterator<Lunchmenu> {
    override fun hasMore(): Boolean {
        return true;

    }

    override fun next(): Lunchmenu? {
        return lunchmenu.lunchmenu[0]

    }

    override fun remove() {
    }
}

class DinerMenu {
    companion object {
        val MAX_LENGS = 6;
    }

    val dinerMenu = arrayOfNulls<DinerMenu?>(MAX_LENGS)

    fun createIterator(): Iterator<DinerMenu> {
        return DinerMenuIterator(this);
    }
}

class Lunchmenu {
    companion object {
        val MAX_LENGS = 6;
    }

    val lunchmenu = ArrayList<Lunchmenu>();
    fun createIterator(): Iterator<Lunchmenu> {
        return LunchMenuIterator(this);
    }

}

class Waitress(val lunchmenu: Lunchmenu, val dinerMenu: DinerMenu) {

    fun printMenu() {
        var lunchMenuIterator = lunchmenu.createIterator();
        var dinerMenuIterator = dinerMenu.createIterator();

        printLunchMenu(lunchMenuIterator)
        printDinerMenu(dinerMenuIterator)

    }

    private fun printLunchMenu(lunchMenuIterator: Iterator<Lunchmenu>) {
        while (lunchMenuIterator.hasMore()) {
            lunchMenuIterator.next();
        }
    }

    private fun printDinerMenu(dinerMenuIterator: Iterator<DinerMenu>) {
        while (dinerMenuIterator.hasMore()) {
            dinerMenuIterator.next();
        }
    }


}

fun main() {
    val lunchmenu = Lunchmenu();
    val dinerMenu = DinerMenu();
    val waitress = Waitress(lunchmenu, dinerMenu);
    waitress.printMenu();
}